{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "import tensorflow as tf\r\n",
    "import numpy as np\r\n",
    "tf.compat.v1.disable_eager_execution()\r\n",
    "x_data = np.random.rand(100)\r\n",
    "y_data = x_data*0.1+0.2\r\n",
    "b = tf.Variable(0.)\r\n",
    "k = tf.Variable(0.)\r\n",
    "y = k*x_data+b\r\n",
    "#二次代阶函数\r\n",
    "loss = tf.reduce_mean(tf.square(y_data-y))\r\n",
    "#定义一个梯度下降法进行训练优化器\r\n",
    "optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.2)\r\n",
    "train = optimizer.minimize(loss)\r\n",
    "init =tf.compat.v1.global_variables_initializer()\r\n",
    "with tf.compat.v1.Session() as sess:\r\n",
    "    sess.run(init)\r\n",
    "    for step in range(201):\r\n",
    "        sess.run(train)\r\n",
    "        if step%20==0:\r\n",
    "            print(step,sess.run([k,b]))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0 [0.04810647, 0.09813472]\n",
      "20 [0.09910523, 0.20043787]\n",
      "40 [0.099509284, 0.20024024]\n",
      "60 [0.099730864, 0.20013176]\n",
      "80 [0.099852405, 0.20007226]\n",
      "100 [0.09991905, 0.20003963]\n",
      "120 [0.099955626, 0.20002173]\n",
      "140 [0.09997566, 0.20001192]\n",
      "160 [0.09998665, 0.20000654]\n",
      "180 [0.09999268, 0.2000036]\n",
      "200 [0.099995986, 0.20000197]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.8",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.8 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "73e03da126b73bfff3642ec5261d56fa25c444ea595de51041687efaa60dda41"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}